home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / UpdateManager / ChangelogViewer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  8.0 KB  |  233 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import pygtk
  5. import gtk
  6. import pango
  7. import subprocess
  8. import os
  9. from gettext import gettext as _
  10.  
  11. class ChangelogViewer(gtk.TextView):
  12.     
  13.     def __init__(self, changelog = None):
  14.         '''Init the ChangelogViewer as an Inheritance of the gtk.TextView'''
  15.         gtk.TextView.__init__(self)
  16.         self.hovering = False
  17.         self.first = True
  18.         self.set_property('editable', False)
  19.         self.set_cursor_visible(False)
  20.         self.set_right_margin(4)
  21.         self.set_left_margin(4)
  22.         self.set_pixels_above_lines(4)
  23.         self.buffer = gtk.TextBuffer()
  24.         self.set_buffer(self.buffer)
  25.         self.connect('button-press-event', self.button_press_event)
  26.         self.connect('motion-notify-event', self.motion_notify_event)
  27.         self.connect('visibility-notify-event', self.visibility_notify_event)
  28.         self.buffer.connect_after('insert-text', self.on_insert_text)
  29.         if changelog != None:
  30.             self.buffer.set_text(changelog)
  31.         
  32.  
  33.     
  34.     def create_context_menu(self, url):
  35.         '''Create the context menu to be displayed when links are right clicked'''
  36.         self.menu = gtk.Menu()
  37.         item_grey_link = gtk.MenuItem(url)
  38.         item_grey_link.connect('activate', self.handle_context_menu, 'open', url)
  39.         item_seperator = gtk.MenuItem()
  40.         item_open_link = gtk.MenuItem(_('Open Link in Browser'))
  41.         item_open_link.connect('activate', self.handle_context_menu, 'open', url)
  42.         item_copy_link = gtk.MenuItem(_('Copy Link to Clipboard'))
  43.         item_copy_link.connect('activate', self.handle_context_menu, 'copy', url)
  44.         self.menu.add(item_grey_link)
  45.         self.menu.add(item_seperator)
  46.         self.menu.add(item_open_link)
  47.         self.menu.add(item_copy_link)
  48.         self.menu.show_all()
  49.  
  50.     
  51.     def handle_context_menu(self, menuitem, action, url):
  52.         """Handle activate event for the links' context menu"""
  53.         if action == 'open':
  54.             self.open_url(url)
  55.         
  56.         if action == 'copy':
  57.             cb = gtk.Clipboard()
  58.             cb.set_text(url)
  59.             cb.store()
  60.         
  61.  
  62.     
  63.     def tag_link(self, start, end, url):
  64.         '''Apply the tag that marks links to the specified buffer selection'''
  65.         tagged = False
  66.         tags = start.get_tags()
  67.         for tag in tags:
  68.             url = tag.get_data('url')
  69.             if url != '':
  70.                 return None
  71.         
  72.         tag = self.buffer.create_tag(None, foreground = 'blue', underline = pango.UNDERLINE_SINGLE)
  73.         tag.set_data('url', url)
  74.         self.buffer.apply_tag(tag, start, end)
  75.  
  76.     
  77.     def on_insert_text(self, buffer, iter_end, text, *args):
  78.         '''Search for http URLs in newly inserted text  
  79.            and tag them accordingly'''
  80.         MALONE = 'https://launchpad.net/bugs/'
  81.         DEBIAN = 'http://bugs.debian.org/'
  82.         CVE = 'http://cve.mitre.org/cgi-bin/cvename.cgi?name='
  83.         ws = [
  84.             ' ',
  85.             '\t',
  86.             '\n']
  87.         brak = [
  88.             ')',
  89.             ']',
  90.             '>']
  91.         punct = [
  92.             ',',
  93.             '!',
  94.             ':']
  95.         dot = [
  96.             '.'] + punct
  97.         search_items = [
  98.             ('http://', ws + brak + punct, 'http://'),
  99.             ('LP#', ws + brak + dot, MALONE),
  100.             ('LP: #', ws + brak + dot, MALONE),
  101.             ('lp: #', ws + brak + dot, MALONE),
  102.             ('LP:#', ws + brak + dot, MALONE),
  103.             ('Malone: #', ws + brak + dot, MALONE),
  104.             ('Malone:#', ws + brak + dot, MALONE),
  105.             ('Ubuntu: #', ws + brak + dot, MALONE),
  106.             ('Ubuntu:#', ws + brak + dot, MALONE),
  107.             ('Closes: #', ws + brak + dot, DEBIAN),
  108.             ('Closes:#', ws + brak + dot, DEBIAN),
  109.             ('closes:#', ws + brak + dot, DEBIAN),
  110.             ('closes: #', ws + brak + dot, DEBIAN),
  111.             ('CVE-', ws + brak + dot, CVE)]
  112.         iter = buffer.get_iter_at_offset(iter_end.get_offset() - len(text))
  113.         iter_real_end = buffer.get_end_iter()
  114.         for start_str, end_list, url_prefix in search_items:
  115.             while True:
  116.                 ret = iter.forward_search(start_str, gtk.TEXT_SEARCH_VISIBLE_ONLY, iter_end)
  117.                 if not ret:
  118.                     break
  119.                 
  120.                 (match_start, match_end) = ret
  121.                 match_suffix = match_end.copy()
  122.                 match_tmp = match_end.copy()
  123.                 while True:
  124.                     if match_tmp.forward_char():
  125.                         text = match_end.get_text(match_tmp)
  126.                         if text in end_list:
  127.                             break
  128.                         
  129.                     else:
  130.                         break
  131.                     match_end = match_tmp.copy()
  132.                 url = url_prefix + match_suffix.get_text(match_end)
  133.                 self.tag_link(match_start, match_end, url)
  134.                 iter = match_end
  135.         
  136.  
  137.     
  138.     def button_press_event(self, text_view, event):
  139.         '''callback for mouse click events'''
  140.         if event.button != 1 and event.button != 3:
  141.             return False
  142.         
  143.         try:
  144.             (start, end) = self.buffer.get_selection_bounds()
  145.         except ValueError:
  146.             event.button != 3
  147.             event.button != 3
  148.         except:
  149.             event.button != 3
  150.  
  151.         if start.get_offset() != end.get_offset():
  152.             return False
  153.         (x, y) = self.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
  154.         iter = self.get_iter_at_location(x, y)
  155.         tags = iter.get_tags()
  156.         for tag in tags:
  157.             url = tag.get_data('url')
  158.             if url != None:
  159.                 if event.button == 3:
  160.                     self.create_context_menu(url)
  161.                     self.menu.popup(None, None, None, event.button, event.time)
  162.                     return True
  163.                 continue
  164.             event.button == 3
  165.         
  166.  
  167.     
  168.     def open_url(self, url):
  169.         '''Open the specified URL in a browser'''
  170.         if os.path.exists('/usr/bin/exo-open'):
  171.             command = [
  172.                 'exo-open',
  173.                 url]
  174.         elif os.path.exists('/usr/bin/gnome-open'):
  175.             command = [
  176.                 'gnome-open',
  177.                 url]
  178.         else:
  179.             command = [
  180.                 'x-www-browser',
  181.                 url]
  182.         if os.getuid() == 0 and os.environ.has_key('SUDO_USER'):
  183.             command = [
  184.                 'sudo',
  185.                 '-u',
  186.                 os.environ['SUDO_USER']] + command
  187.         
  188.         subprocess.Popen(command)
  189.  
  190.     
  191.     def motion_notify_event(self, text_view, event):
  192.         '''callback for the mouse movement event, that calls the
  193.            check_hovering method with the mouse postition coordiantes'''
  194.         (x, y) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, int(event.x), int(event.y))
  195.         self.check_hovering(x, y)
  196.         self.window.get_pointer()
  197.         return False
  198.  
  199.     
  200.     def visibility_notify_event(self, text_view, event):
  201.         '''callback if the widgets gets visible (e.g. moves to the foreground)
  202.            that calls the check_hovering method with the mouse position
  203.            coordinates'''
  204.         (wx, wy, mod) = text_view.window.get_pointer()
  205.         (bx, by) = text_view.window_to_buffer_coords(gtk.TEXT_WINDOW_WIDGET, wx, wy)
  206.         self.check_hovering(bx, by)
  207.         return False
  208.  
  209.     
  210.     def check_hovering(self, x, y):
  211.         '''Check if the mouse is above a tagged link and if yes show
  212.            a hand cursor'''
  213.         _hovering = False
  214.         iter = self.get_iter_at_location(x, y)
  215.         tags = iter.get_tags()
  216.         for tag in tags:
  217.             url = tag.get_data('url')
  218.             if url != None:
  219.                 _hovering = True
  220.                 break
  221.                 continue
  222.         
  223.         if _hovering != self.hovering or self.first == True:
  224.             self.first = False
  225.             self.hovering = _hovering
  226.             if self.hovering:
  227.                 self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))
  228.             else:
  229.                 self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR))
  230.         
  231.  
  232.  
  233.